package com.sanjiahantang.youjiaclient.module.common.models.impl;

import android.content.Context;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.common.beans.HelpBean;
import com.sanjiahantang.youjiaclient.module.login.beans.LoginBean;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageBean;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageNoticeCountBean;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageUseNoticeBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.beans.UserBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.complain.beans.ComplainBean;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.EvaGoodsBean;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.EvaServiceBean;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressDetailBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.OrderBean;
import com.sanjiahantang.youjiaclient.module.me.scheme.beans.MeCaseOBean;
import com.sanjiahantang.youjiaclient.module.me.scheme.beans.SchemeBean;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;


/**
 * Created by zy on 2017/9/4.
 */

public class UserModel implements IUserModel {

    @Override
    public void loadUserInfo(final Context context, Map<String,String> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/findToken", params,
                new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void doLogin(final Context context, Map<String, String> params, final CallBack<LoginBean> callBack ,String tag) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL
                + "/user/phone/customer/loginForPassword", params, new BaseDelegate.ResultCallback<LoginBean>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(LoginBean response, Object tag) {
                callBack.onSuccess(response,context,tag);
            }
        },true,tag);
    }

    @Override
    public void doChangePassword(final Context context, Map<String, Object> params,final CallBack<StringBean> callBack) {

        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/customer/modifyPwd",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void getUserMessage(final Context context, Map<String, String> params,final CallBack<MessageBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/system/phone/notice/searchNoticeForCustomer",
                params, new BaseDelegate.ResultCallback<MessageBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(MessageBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
//                        Log.e("11",response.getData());
                    }
                });
    }

    @Override
    public void getUseNoticeForCustomer(final Context context, Map<String, Object> params, final CallBack<MessageUseNoticeBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/system/phone/notice/saveSystemUseNoticeForCustomer",
                params, new BaseDelegate.ResultCallback<MessageUseNoticeBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(MessageUseNoticeBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void getSearchNoticeCountForCustomer(final Context context, Map<String, String> params, final CallBack<MessageNoticeCountBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/system/phone/notice/searchNoticeCountForCustomer",
                params, new BaseDelegate.ResultCallback<MessageNoticeCountBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(MessageNoticeCountBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void loadServiceEvaData(final Context context, final CallBack<EvaServiceBean> callBack) {

    }

    @Override
    public void loadGoodsEvaData(final Context context, final CallBack<EvaGoodsBean> callBack) {

    }

    @Override
    public void loadMeComplainList(final Context context, final int type, final CallBack<ComplainBean> callBack) {

    }
    //我的方案
    @Override
    public void loadSchemeList(final Context context, Map<String, String> params, final CallBack<MeCaseOBean.InfoListData > callBack) {

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/project/phone/plan/searchProjectPlanInfoPhone", params,
                new BaseDelegate.ResultCallback<MeCaseOBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(MeCaseOBean response, Object tag) {
                        if(response.getCode() == RESPONSE_CODE_SUC){
                            List<MeCaseOBean.InformationListBean> informationList = response.getData().getInformationList();
                            List<Integer> idList = new ArrayList<Integer>();
                            List<Integer> tempList = new ArrayList<Integer>();
                            for (MeCaseOBean.InformationListBean bean : informationList){
                                tempList.add(bean.getId());
                            }
                            idList = Utils.removeDuplicate(tempList);
                            List<List<MeCaseOBean.InformationListBean>> infoList = new ArrayList<List<MeCaseOBean.InformationListBean>>();
                            List<String> nameList = new ArrayList<String>();
                            for (int i = 0,max = idList.size(); i < max;i++){
                                List<MeCaseOBean.InformationListBean> itemList = new ArrayList<MeCaseOBean.InformationListBean>();
                                for (MeCaseOBean.InformationListBean bean : informationList){
                                    if (idList.get(i) == bean.getId()){
                                        itemList.add(bean);
                                    }
                                }

                                infoList.add(itemList);
                                if (itemList.size() > 0){
                                    nameList.add(itemList.get(0).getApartmentName());
                                }else{
                                    nameList.add("小区");
                                }
                            }
                            MeCaseOBean.InfoListData data = new MeCaseOBean.InfoListData();
                            data.setList(infoList);
                            data.setNameList(nameList);
                            callBack.onSuccess(data,context,tag);

                        }

                    }
                },true);

    }

    @Override
    public void loadSchemeDetail(final Context context, Map<String, String> params, final CallBack<SchemeBean> callBack) {

//        if(ConfigValue.isDebug){
//            new Handler().postDelayed(new Runnable() {
//                public void run() {
//                    SchemeBean  bean=new SchemeBean();
//                    bean.setCode(200);
//                    bean.setMessage("成功");
//                    List<SchemeBean.Data> dataList = new ArrayList<SchemeBean.Data>();
//                    for (int i = 0;i<10;i++){
//                        SchemeBean.Data data1=new SchemeBean.Data();
//                        data1.setRoomType("两室一厅，欧式"+i);
//                        dataList.add(data1);
//                    }
//                    bean.setData(dataList);
//                    callBack.onSuccess(bean,context, null);
//                }
//            }, 1000);
//            return;
//        }
    }

    @Override
    public void getAddressList(final Context context, Map<String, String> params, final CallBack<AddressBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customerAddress/searchUserAddress", params,
                new BaseDelegate.ResultCallback<AddressBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(AddressBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void deleteAddress(final Context context, Map<String, String> params, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customerAddress/deleteUserAddress", params,
                new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void saveAddress(final Context context, Map<String, Object> params, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/customerAddress/saveUserAddress", params,
                new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void loadAddressDetail(final Context context, Map<String, String> params, final CallBack<AddressDetailBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customerAddress/indexUserAddress", params,
                new BaseDelegate.ResultCallback<AddressDetailBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(AddressDetailBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }



    @Override
    public void getCreatedOrder(final Context context, Map<String, String> params, final CallBack<OrderBean> callBack) {

    }

    @Override
    public void editDefaultAddress(final Context context, Map<String, Object> params, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/customerAddress/saveIsDefault", params,
                new BaseDelegate.ResultCallback<BaseBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }


    @Override
    public void dopersonData(final Context context, Map<String, String> params, final CallBack<UserBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/findCustomerInfoPhone", params,
                new BaseDelegate.ResultCallback<UserBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(UserBean response, Object tag) {
                        callBack.onSuccess(response, context,tag);
                    }
                });

    }

    //发送注册验证码成功
    @Override
    public void senMessageCode(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/getRegisterCustomerIdentifyingCode",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }

    @Override
    public void doforgetPassword(final Context context, Map<String, String> params,final CallBack<StringBean> callBack) {

        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/findPassword",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                },true);
    }
    //更新个人数据嘻嘻
    @Override
    public void updatePerson(final Context context, Map<String, Object> params, final CallBack<StringBean> callBack) {

        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/customer/updateCustomerInfo",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }
    //获取职业信息
    @Override
    public void updateUserDuty(final Context context, Map<String, String> params, final CallBack<UserBean> callBack) {


        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/system/dic/findDicByParentForPhone",
                params, new BaseDelegate.ResultCallback<UserBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(UserBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }

    @Override
    public void loadHelpList(final Context context, Map<String, Object> param, final CallBack<HelpBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/article/phone/searchArHelpInfoPhone",
                param, new BaseDelegate.ResultCallback<HelpBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(HelpBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);
                    }
                });
    }

    @Override
    public void loadHelpArticle(final Context context, Map<String, String> param, final CallBack<HelpBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/article/phone/findArHelpByIdPhone",
                param, new BaseDelegate.ResultCallback<HelpBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(HelpBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }

    @Override
    public void findPassword(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/findPassword",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }

    @Override
    public void getRegisterCustomerPassWordCode(final Context context, Map<String, String> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/customer/getRegisterCustomerPassWordCode",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }

    @Override
    public void findNoticeInfoById(final Context context, Map<String, String> param, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/system/notice/findNoticeInfoById",
                param, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }

    @Override
    public void modifyPayPwd(final Context context, Map<String, Object> params, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/user/phone/customer/modifyPayPwd",
                params, new BaseDelegate.ResultCallback<StringBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                });
    }


}
